package com.yifan.service;

import com.yifan.dao.BaseDao;
import com.yifan.entity.Software;
import com.yifan.utils.JdbcUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SoftwareService {

	BaseDao dao = new BaseDao();

	// 获取列表
	public Map<String, Object> getSoftwares(String name, int page, int pageSize) {
		String sql = "select *  from t_software where 1=1 ";
		String countsql = "select count(*) from t_software where 1=1 ";
		List<Object> list = new ArrayList<Object>();
		if (name != null && name != "") {
			sql += " and name = ? ";
			countsql += " and name = ? ";
			list.add(name);
		}
		int count = 0;
		List<Map<String, Object>> objects = null;
		try {
			count = dao.countObject(JdbcUtils.getconn(), countsql, list);

			sql += "  limit ?,?";
			list.add((page - 1) * pageSize);
			list.add(pageSize);

			objects = dao.getObjectsRMap(JdbcUtils.getconn(), sql, list);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("data", objects);
		map.put("count", count);
		map.put("msg", "列表信息");
		map.put("code", "0");
		return map;
	}

	// 添加
	public Map<String, String> addSoftware(Software software) {
		Map<String, String> map = new HashMap<String, String>();
		String sql = "insert into t_software (name, company, type, price, size, createDate) " +
				"values (?,?,?,?,?,?)";
		try {
			dao.addObject(JdbcUtils.getconn(), sql, new Object[] {
					software.getName(),
					software.getCompany(),
					software.getType(),
					software.getPrice(),
					software.getSize(),
					software.getCreateDate()});
			map.put("msg", "ok");
			map.put("handle", "添加成功！");
		} catch (SQLException e) {
			e.printStackTrace();
			map.put("msg", "nok");
			map.put("handle", "发生错误了！");
		}
		return map;
	}

	// 删除
	public Map<String, String> deleteSoftware(int id) {
		Map<String, String> map = new HashMap<String, String>();
		String sql = "delete from t_software where id=?";
		try {
			dao.deleteObject(JdbcUtils.getconn(), sql, new Object[] { id });
			map.put("msg", "ok");
			map.put("handle", "删除成功！");
		} catch (SQLException e) {
			e.printStackTrace();
			map.put("msg", "nok");
			map.put("handle", "发生错误了！");
		}
		return map;
	}

	// 批量删除
	public Map<String, String> deleteSoftwares(String[] idarr) {
		Map<String, String> map = new HashMap<String, String>();
		for (int i = 0; i < idarr.length; i++) {
			map = deleteSoftware(Integer.parseInt(idarr[i]));
		}
		return map;
	}

	// 修改菜单
	public Map<String, String> updateSoftware(Software software) {
		Map<String, String> map = new HashMap<String, String>();
		String sql = "update t_software set name=?, company=?, type=?, price=?, size=?, createDate=? where id=?";
		try {
			dao.updateObject(JdbcUtils.getconn(), sql, new Object[] {
					software.getName(),
					software.getCompany(),
					software.getType(),
					software.getPrice(),
					software.getSize(),
					software.getCreateDate(),
	
					software.getId() });
			map.put("msg", "ok");
			map.put("handle", "修改成功！");
		} catch (SQLException e) {
			e.printStackTrace();
			map.put("msg", "nok");
			map.put("handle", "发生错误了！");
		}
		return map;
	}

	// 获取单个
	public Map<String, Object> getSoftware(int id) {
		String sql = "select * from t_software  where id=?";
		Map<String, Object> object = null;
		try {
			object = dao.getObjectRMap(JdbcUtils.getconn(), sql, new Object[] { id });
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return object;
	}

	public List<Software> getListSoftware(List<Object> obj) {
		List<Software> list = new ArrayList<Software>();
		for (int i = 0; i < obj.size(); i++) {
			Software software = (Software) obj.get(i);
			list.add(software);
		}
		return list;
	}

}
